<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //   console.log([] instanceof Object);//true
      //   console.log(String instanceof Object);//true
      function myNew(fn, ...args) {
        //首先创建一个空对象
        let obj = {};
        //设置原型
        obj.__proto__ = fn.prototype;
        // 绑定this
        let res = fn.apply(obj, args);
        // 确保返回对象(是值返回对象，是引用类型返回引用的对象)
        return res instanceof Object ? res : obj;
      }

      function person(name) {
        this.name = name;
        return { name: "返回引用类型" };
      }

      const a = myNew(person, "zyf");
      console.log(a.name);
    </script>
  </body>
</html>
